<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 五福支付
 */
class WufuzhifuController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        //$product = M("Product")->where(["id"=> I("request.pay_bankcode","")])->find();
        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );


        $data = array();
        $data['amt'] = $return['amount']*100;
        $data['merData'] = 'zf';
        $data['merId'] = $this->config['mch_id'];
        $data['merchOrderId'] = $return['orderid'];
        $data['notifyUrl'] = $return['notifyurl'];
        $data['pName'] = 'pizza';
        $data['retUrl'] = $return['callbackurl'];
        $data['showCashier'] = '0';
        $data['svcName'] = 'UniThirdPay';

        switch($this->config['paytype']){
            //微信H5
            case 2:
                $data['tranType'] = 'WEIXIN_H5';
                break;
            //支付宝H5
            case 4:
                $data['tranType'] = 'ALIPAY_H5';
                break;
            //QQ
            case 8:
                $data['tranType'] = 'QQ_NATIVE';
                break;
            //QQH5
            case 10:
                $data['tranType'] = 'QQ_NATIVE';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        ksort($data);

        $str = '';
        foreach($data as $k=>$v){
            $str .= $v;
        }
        $str .= $this->config['signkey'];
        $data['md5value'] = strtoupper(md5($str));

        if(in_array($this->config['paytype'],[8,10])){
            //qq,qqh5
            $res = curl($this->config['gateway'],httpstr($data));
        }else{
            po($this->config['gateway'], $data);
        }

        $res = json_decode($res, true);

        $code = $res['payUrl'];
        if(!$code){
            if($res['retMsg']){
                $this->showmessage($res['retMsg'],['code'=>C("THIRDCODES.".$this->class)]);
            }
            $this->showmessage('未获取到支付链接!',['code'=>C("THIRDCODES.".$this->class)]);
        }


        $paytype = transArr( C("PAYTYPES"), "id");

        $this->assign("wap_url", $code);
        //$this->assign("code_url", $res['data']['payurl']);
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=" . base64_encode($code));

        switch($this->config['paytype']){
            //微信
            case 1:
                $this->display("WeiXin/weixin");
                break;
            case 3:
            case 4:
            case 17:
            case 18:
                if(isMobile()){
                    $this->display("WeiXin/alipaywap");
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            //qqH5
            case 8:
                if(isMobile()){
                    $this->display("WeiXin/qqwap");
                }else{
                    $this->display("WeiXin/qq");
                }
                break;
            case 10:
                if(isMobile()){
                    $this->display("WeiXin/qqwap");
                }else{
                    $this->display("WeiXin/qq");
                }
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    //通知
    public function notifyurl(){
        //file_put_contents("a.txt",var_export($_POST,true)."\r\n\r\n");
        //file_put_contents("a.txt",var_export($_GET,true)."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", date("Y-m-d H:i:s",time())."\r\n\r\n", FILE_APPEND);

        $p = I("post.");

        /*$p = array (
            'tranTime' => '20180921 23:16:34',
            'orderStatusMsg' => '交易成功',
            'merchOrderId' => '201809212316262214479',
            'merData' => 'zf',
            'orderId' => 'I201809210027963287',
            'amt' => '10000',
            'status' => '0',
            'md5value' => 'A60192A13BFA3F2FDCECF954687F8289',
        );*/

        if(empty($p)){
            exit("no_data");
        }

        $sign = $p['md5value'];

        //获取订单信息
        $orderid = $p['merchOrderId'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "server_return"=> file_get_contents('php://input'),
            "pay_orderid_rd"=>$p['orderId']
        ));

        //获取渠道信息
        $this->config = Channel::getInstance()->find(['code'=>$this->class]);
        if(!$this->config){
            $this->config = M("Channel")->where(array('code'=>$this->class))->find();
        }

        if(!$this->config){
            exit("get_tongdao_fail");
        }

        if(!$p || empty($p)){
            exit("wrong");
        }

        if($p['status'] != 0){
            exit("pay_fail");
        }


        unset($p['md5value']);
        ksort($p);

        $str = "";
        foreach($p as  $k=>$v){
            $str .= $v;
        }
        $str .= $this->config['signkey'];

        $sign2 = strtoupper(md5($str));

        //echo 'sign: '.$sign.'<br/>';
        //echo 'sign2:'.$sign2.'<br/>'; exit;
        if($sign != $sign2){
            echo 'sign_wrong';exit;
        }

        $this->async($find['pay_orderid'],'SUCCESS');
    }

    public function callBackUrl(){
        echo '处理成功'; exit;
    }

    public function getBankName($code){
        $ret = '';

        //农业银行 ABC 建设银行 CCB 兴业银行 CIB 招商银行 CMB 民生银行 CMBC 华夏银行 HXB
        //工商银行 ICBC 上海银行 BOSH 中国银行 BOC 交通银行 COMM 上海浦东发展银行 SPDB 广东发展银行 GDB 平安银行 PINGAN 邮政储蓄 PSBC 上海农村商业银行 SHRCB 北京银行 BOB 广州商业银行 GZCB 光大银行 CEB 温州银行 BOWZ 中信银行 CNCB
        switch($code){
            //北京银行
            case 'BOB':
                $ret = 'BOB';
                break;
            //东亚银行
            case 'BEA':
                $ret = '';
                break;
            //中国工商银行
            case 'ICBC':
                $ret = 'ICBC';
                break;
            //光大银行
            case 'CEB':
                $ret='CEB';
                break;
            //广发银行
            case 'CGB':
                $ret='GDB';
                break;
            //华夏银行
            case 'HXB':
                $ret='HXB';
                break;
            //中国建设银行
            case 'CCB':
                $ret='CCB';
                break;
            //交通银行
            case 'BOCOM':
                $ret='COMM';
                break;
            //中国民生银行
            case 'CMBC':
                $ret='CMBC';
                break;
            //南京银行
            case 'NJCB':
                $ret='';
                break;
            //宁波银行
            case 'NBCB':
                $ret='';
                break;
            //中国农业银行
            case 'ABC':
                $ret='ABC';
                break;
            //平安银行
            case 'PAB':
                $ret='PINGAN';
                break;
            //上海银行
            case 'SHB':
                $ret='';
                break;
            //上海浦东发展银行
            case 'SPDB':
                $ret='SPDB';
                break;
            //深圳发展银行
            case 'SDB':
                $ret='';
                break;
            //兴业银行
            case 'CIB':
                $ret='CIB';
                break;
            //中国邮政储蓄银行
            case 'PSBS':
                $ret='PSBC';
                break;
            //招商银行
            case 'CMB':
                $ret='CMB';
                break;
            //浙商银行
            case 'CZB':
                $ret='';
                break;
            //中国银行
            case 'BOC':
                $ret='BOC';
                break;
            //中信银行
            case 'CTTIC':
                $ret='CNCB';
                break;
            default:
                $this->showmessage('暂不支持此银行卡!',['code'=>'024']);
        }
        if(!$ret){
            $this->showmessage('暂不支持此银行卡!',['code'=>'024']);
        }

        return $ret;
    }
}